package com.ego.admin.controller;

import com.ego.admin.po.SysDepartment;
import com.ego.admin.service.SysDepartmentService;
import com.ego.common.annotation.ControllerEndpoint;
import com.ego.common.util.Result;
import com.ego.common.vo.BaseSearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @program: ego
 * @description: 部门
 * @author: KamTang
 * @create: 2023-02-23 21:27
 **/
@RequestMapping("/sys/department")
@RestController
public class SysDepartmentController {

    @Autowired
    private SysDepartmentService departmentService;

    /**
     * 展示部门
     */
    @GetMapping("/list")
    public Result departmentList (@RequestParam(required = false) String name) {
        return Result.ok().put("departmentList", departmentService.list(name));
    }

    /**
     * 删除部门
     */
    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @PostMapping("/delete")
    public Result delete (@Valid @RequestBody Long[] ids) {
        departmentService.deleteBatch(ids);
        return Result.ok();
    }

    /**
     * 根据部门id获取部门信息
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id) {
        SysDepartment department = departmentService.getById(id);
        return Result.ok().put("department", department);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @ControllerEndpoint(exceptionMessage = "新增部门失败", operation = "新增部门")
    public Result add (@RequestBody SysDepartment department) {
        boolean flag = departmentService.save(department);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更新
     */
    @ControllerEndpoint(exceptionMessage = "更新部门失败", operation = "更新部门")
    @PostMapping("/update")
    public Result update (@RequestBody SysDepartment department) {
        boolean flag = departmentService.updateById(department);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}
